package com.project.market.service.impl;

import com.github.pagehelper.PageHelper;
import com.project.market.db.domain.MarketAddress;
import com.project.market.db.domain.MarketAddressExample;
import com.project.market.db.mapper.MarketAddressMapper;
import com.project.market.db.mapper.MarketKeywordMapper;
import com.project.market.service.AdminAddressService;
import com.project.market.util.MybatisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Struct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminAddressServiceImpl implements AdminAddressService {
    @Override
    public List<MarketAddress> list(String page, String limit, String sort, String order, String name, String userId) {

        SqlSession session = MybatisUtil.getSession();
        MarketAddressMapper addressMapper = session.getMapper(MarketAddressMapper.class);

        MarketAddressExample addressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null && !("".equals(userId))) {
            try {
                criteria.andUserIdEqualTo(Integer.valueOf(userId));
            } catch (Exception e) {
                return null;
            }
        }
        addressExample.setOrderByClause(sort + " " + order);
        List<MarketAddress> list = addressMapper.selectByExample(addressExample);

        PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));

        session.commit();
        session.close();

        return list;
    }
}
